/**
 * @author liubaohui
 * @desc 时间格式化
 * @param {string|number} time  时间字符串，如 '2018/11/11 12:12:12'
 * @param {string} formatter 需要的格式 默认为 'YYYY-MM-DD'
 * @returns {string}
 */
export default function timeFormat(time: string | number, formatter: string = 'YYYY-MM-DD') {
  if (!time) return '';
  let date = new Date(time);
  let year, month, day, hour, minute, second;

  year = date.getFullYear().toString();
  month = (date.getMonth() + 1).toString();
  day = date.getDate().toString();
  hour = date.getHours().toString();
  minute = date.getMinutes().toString();
  second = date.getSeconds().toString();

  return formatter
    .replace('YYYY',  year)
    .replace('MM', month[1] ? month : `0${month}`)
    .replace('DD', day[1] ? day : `0${day}`)
    .replace('hh', hour[1] ? hour : `0${hour}`)
    .replace('mm', minute[1] ? minute : `0${minute}`)
    .replace('ss', second[1] ? second : `0${second}`)
}
